package com.tinytoon.hunghoaqua;

import java.util.Random;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;

public class Ball {
	/**
	 * Location: where the ball appears
	 */
	public int mLocation;
	public static final int LOCATION_LEFT = 0;
	public static final int LOCATION_RIGHT = 1;
	public static final int LOCATION_LEFT_TOP = 2;
	public static final int LOCATION_RIGHT_TOP = 3;
	public static final int LOCATION_TOP = 4;

	public int mType;
	public static final int TYPE_NORMAL = 0;
	public static final int TYPE_FINISH_ANIMATION = 1;
	public static final int TYPE_RED_BORDER = 2;
	public static final int TYPE_SCORE_MINUS = 3;

	public int mAnimatioStep = 0;
	public static final int STEP_ANIMATION_START = 0;
	public static final int STEP_ANIMATION_INTERVAL = 5;

	public float mX;
	public float mY;
	public float mSpeedX;
	public float mSpeedY;
	private boolean isOutOfScreen = false;
	public Bitmap mBitmap;

	public Ball(Resources res) {
		Random rand = new Random();
		mBitmap = GameRes.getBitmap(res, "ball");

		// init ball's location
		mLocation = rand.nextInt(5);
		switch (mLocation) {
		case LOCATION_LEFT:
			mX = 0 - mBitmap.getWidth();
			mY = Panel.mHeight - 2*mBitmap.getHeight();
			mSpeedX = 8;
			mSpeedY = 0;
			break;
		case LOCATION_RIGHT:
			mX = Panel.mWidth;
			mY = Panel.mHeight - 2f*mBitmap.getHeight();
			mSpeedX = -8;
			mSpeedY = 0;
			break;
		case LOCATION_TOP:
			mX = rand.nextInt((int) Panel.mWidth - mBitmap.getWidth());
			mY = -mBitmap.getHeight();
			mSpeedX = 0;
			mSpeedY = 4;
			break;
		case LOCATION_LEFT_TOP:
			mX = rand.nextInt((int) Panel.mWidth - mBitmap.getWidth());
			mY = -mBitmap.getHeight();
			mSpeedX = 8;
			mSpeedY = 4;
			break;
		case LOCATION_RIGHT_TOP:
			mX = rand.nextInt((int) Panel.mWidth - mBitmap.getWidth());
			mY = -mBitmap.getHeight();
			mSpeedX = 8;
			mSpeedY = 4;
			break;
		default:
			break;
		}
		mType = TYPE_NORMAL;
	}

	public int getAnimationStep() {
		return mAnimatioStep;
	}

	public void setAnimationStep(int mExplosionStep) {
		this.mAnimatioStep = mExplosionStep;
	}

	public void doDraw(Canvas canvas) {
		canvas.drawBitmap(mBitmap, mX, mY, null);
	}

	public void animate() {
		mX += mSpeedX;
		mY += mSpeedY;
		checkBorders();
	}

	private void checkBorders() {
		switch (mLocation) {
		// move from left to right
		case LOCATION_LEFT:
			// right border
			if (mX >= Panel.mWidth)
				isOutOfScreen = true;
			break;
		// move from right to left
		case LOCATION_RIGHT:
			if (mX <= -mBitmap.getWidth())
				isOutOfScreen = true;
			break;
		// move from top to bottom
		default:
			// left border
			if (mX <= 0) {
				mSpeedX = -mSpeedX;
				mX = 0;
			}
			// right border
			else if (mX + mBitmap.getWidth() >= Panel.mWidth) {
				mSpeedX = -mSpeedX;
				mX = Panel.mWidth - mBitmap.getWidth();
			}

			// bottom
			if (mY >= Panel.mHeight) {
				isOutOfScreen = true;
			}
			break;
		}

	}

	public boolean isOutOfScreen() {
		return isOutOfScreen;
	}

	public Rect getRect() {
		int left = (int) mX;
		int top = (int) mY;
		int right = (int) (mX + mBitmap.getWidth());
		int bottom = (int) (mY + mBitmap.getHeight());
		return new Rect(left, top, right, bottom);
	}
}
